import { createRouter, createWebHistory, Router } from 'vue-router'
import { Routes } from './router.config'

// 路由实例
const router: Router = createRouter({
	history: createWebHistory(),
	routes: Routes
})

router.beforeEach((to, from, next) => {
	const commonStore = localStorage.getItem('common')
	const userInfo = JSON.parse(commonStore)?.userInfo
	if (to.path === '/login') next()
	if (!userInfo) {
		next({ path: '/login' })
	} else {
		next()
	}
})

export default router
